import numpy as np
import cv2 as cv

class MixUpConvert(object):
    
    # img_type : uint8 范围是【0-255】, float 范围是 【0-1】
    def __init__(self, is_random = True, img_type = 'uint8'):
        self.is_random = is_random
        self.__r = 0.2
        self.img_type = img_type
    
    def setR(self, r):
        self.__r = r
    
    # 转换
    def convert(self, imgs, boxes, points):
        if self.is_random:
            self.__r = np.random.beta(0.2, 0.5)
        self.__r = self.__r if self.__r <= 1 else 1 
        img = imgs[0]
        for i in range(1, len(imgs)):
            other = imgs[i]
            # 融合背景
            img = self.__r * img + (1 - self.__r) * other
        
        if self.img_type == 'uint8':
            img = (img / np.max(img) * 255.0).astype(np.uint8)
        else:
            img = img / np.max(img)
        
        if boxes is not None:
            boxes = np.concatenate(boxes)
        if points is not None:            
            points = np.concatenate(points)
        return img, boxes, points
        


    
    